package travelDream.beans;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import traveldream.ejb.DTO.ExcursionDTO;
import traveldream.ejb.DTO.GiftListDTO;
import traveldream.ejb.DTO.HotelDTO;
import traveldream.ejb.DTO.PackageDTO;
import traveldream.ejb.DTO.TransportationDTO;
import traveldream.ejb.interfaces.ExcursionBeanLocal;
import traveldream.ejb.interfaces.GiftListBeanLocal;
import traveldream.ejb.interfaces.HotelBeanLocal;
import traveldream.ejb.interfaces.NotFindException;
import traveldream.ejb.interfaces.TransportationBeanLocal;

@ManagedBean(name = "GiftManaged")
@SessionScoped
public class GiftListManaged {
	private List<GiftListDTO> glDTO;
	private List<GiftListDTO> filteredPackage;
	
	private GiftListDTO viewedP;
	
	private HotelDTO hDTO;
	private int selectedH;
	private ExcursionDTO eDTO;
	private int selectedE;
	private TransportationDTO gDTO;
	private int selectedG;
	private TransportationDTO rDTO;
	private int selectedR;
	
	private String link;
	
	private boolean notPrima = false;
	
	@EJB
	GiftListBeanLocal GiftListBeanLocal;
	
	@EJB
	private HotelBeanLocal HotelBeanLocal;
	@EJB
	private TransportationBeanLocal TransportationBeanLocal;
	@EJB
	private ExcursionBeanLocal ExcursionBeanLocal;
	
	public void allPackage(){
		glDTO = GiftListBeanLocal.allPackage();
	}
	
	public void searchById() {
	
			FacesContext facesContext = FacesContext.getCurrentInstance();
			if(!facesContext.getExternalContext().getRequestParameterMap().isEmpty()){
				int passedParameter = Integer.parseInt((facesContext.getExternalContext().getRequestParameterMap().get("id")));

			try{
			viewedP = GiftListBeanLocal.find(passedParameter);
			}
			catch(NotFindException e){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Gift List not founded. Please, use the menu to go back to gift lists and try again!!"));
				return;
			}
			try{
			hDTO = HotelBeanLocal.find(viewedP.getIdHotel());
			selectedH = hDTO.getId();
			rDTO = TransportationBeanLocal.find(viewedP.getIdReturn());
			selectedR = rDTO.getId();
			gDTO = TransportationBeanLocal.find(viewedP.getIdGoing());
			selectedG = gDTO.getId();
			}
			catch(NotFindException e){
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Please, go back and try again!!"));
			}
			try
			{
			eDTO = ExcursionBeanLocal.find(viewedP.getIdExcursion());
			selectedE = eDTO.getId();
			}
			catch(NotFindException e){
				if(viewedP.getIdExcursion()>0)
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Please, use the menu to go back to excursions and try again!!"));
			}
			link = "http://localhost:8080/travelDreamWeb/giftPackage.xhtml?id="+viewedP.getId();
			notPrima = true;
			}
		
	}
	
	public String changePage(int id){
		System.out.println(id);
		return "giftView.xhtml?faces-redirect=true&id="+id;
	}
	
	public String gift(char c){
		switch (c){
			case 'g':
				if(!GiftListBeanLocal.checkAvailability(viewedP)){
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "We are sorry, but the available places for one of the basic products of this gift list are 0.")); 
					return "";
				}
				return "user/goingGiftConfirm.xhtml?faces-redirect=true&id="+viewedP.getId();
			case 'h':
				if(!GiftListBeanLocal.checkAvailability(viewedP)){
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "We are sorry, but the available places for one of the basic products of this gift list are 0.")); 
					return "";
				}
				return "user/hotelGiftConfirm.xhtml?faces-redirect=true&id="+viewedP.getId();
			case 'r':
				if(!GiftListBeanLocal.checkAvailability(viewedP)){
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "We are sorry, but the available places for one of the basic products of this gift list are 0.")); 
					return "";
				}
				return "user/returnGiftConfirm.xhtml?faces-redirect=true&id="+viewedP.getId();
			case 'e':
				if(!GiftListBeanLocal.checkAvailability(viewedP)){
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "We are sorry, but the available places for one of the basic products of this gift list are 0.")); 
					return "";
				}
				if(viewedP.getIdExcursion()!=0)
					return "user/excursionGiftConfirm.xhtml?faces-redirect=true&id="+viewedP.getId();
				else
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "You cannot buy something which doesn't exist!!")); 
			default:
				return "";
		}
	}
	
	public String giftHotel(){
		try{
		if(GiftListBeanLocal.purchaseHotel(viewedP)){
			return "home.xhtml?faces-redirect=true";
		}
		else{
			//notify the error
			 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "The hotel has already been gifted"));  
		}
		}
		catch(NotFindException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Gift List not founded. Please, go back and try again"));
		}
		return "";
		
	}
	public String giftGoing(){
		try{
		if(GiftListBeanLocal.purchaseGoing(viewedP)){
			return "home.xhtml?faces-redirect=true";
		}
		else{
			//notify the error
			 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "The going transport has already been gifted"));  
		}
		}
		catch(NotFindException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Gift List not founded. Please, go back and try again"));
		}
		return "";
		
	}
	public String giftReturn(){
		try{
		if(GiftListBeanLocal.purchaseReturn(viewedP)){
			return "home.xhtml?faces-redirect=true";
		}
		else{
			//notify the error
			 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "The return transport has already been gifted"));  
		}
		}
		catch(NotFindException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Gift List not founded. Please, go back and try again"));
		}
		return "";
		
	}
	public String giftExcursion(){
		try{
		if(GiftListBeanLocal.purchaseExcursion(viewedP)){
			return "home.xhtml?faces-redirect=true";
		}
		else{
			//notify the error
			 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "The excursion has already been gifted"));  
		}
		}
		catch(NotFindException e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Gift List not founded. Please, go back and try again"));
		}
		return "";
		
	}

	public List<GiftListDTO> getGlDTO() {
		return glDTO;
	}

	public void setGlDTO(List<GiftListDTO> glDTO) {
		this.glDTO = glDTO;
	}

	public List<GiftListDTO> getFilteredPackage() {
		return filteredPackage;
	}

	public void setFilteredPackage(List<GiftListDTO> filteredPackage) {
		this.filteredPackage = filteredPackage;
	}

	public GiftListDTO getViewedP() {
		return viewedP;
	}

	public void setViewedP(GiftListDTO viewedP) {
		this.viewedP = viewedP;
	}

	public HotelDTO gethDTO() {
		return hDTO;
	}

	public void sethDTO(HotelDTO hDTO) {
		this.hDTO = hDTO;
	}

	public int getSelectedH() {
		return selectedH;
	}

	public void setSelectedH(int selectedH) {
		this.selectedH = selectedH;
	}

	public ExcursionDTO geteDTO() {
		return eDTO;
	}

	public void seteDTO(ExcursionDTO eDTO) {
		this.eDTO = eDTO;
	}

	public int getSelectedE() {
		return selectedE;
	}

	public void setSelectedE(int selectedE) {
		this.selectedE = selectedE;
	}

	public TransportationDTO getgDTO() {
		return gDTO;
	}

	public void setgDTO(TransportationDTO gDTO) {
		this.gDTO = gDTO;
	}

	public int getSelectedG() {
		return selectedG;
	}

	public void setSelectedG(int selectedG) {
		this.selectedG = selectedG;
	}

	public TransportationDTO getrDTO() {
		return rDTO;
	}

	public void setrDTO(TransportationDTO rDTO) {
		this.rDTO = rDTO;
	}

	public int getSelectedR() {
		return selectedR;
	}

	public void setSelectedR(int selectedR) {
		this.selectedR = selectedR;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}
	
	
	
	
}
